package com.yan_jiu_sheng.LeetCodeHot100.AC;

/**
 * https://leetcode.cn/problems/maximum-depth-of-binary-tree/?envType=study-plan-v2&envId=top-100-liked
 * 通过
 *
 * @author yulongTian
 * @create 2024-06-01 13:52
 */
public class Test37 {
    public static void main(String[] args) {
        TreeNode treeNode3 = new Test37().new TreeNode(3);
        TreeNode treeNode9 = new Test37().new TreeNode(9);
        TreeNode treeNode20 = new Test37().new TreeNode(20);
        TreeNode treeNode15 = new Test37().new TreeNode(15);
        TreeNode treeNode7 = new Test37().new TreeNode(7);
        treeNode3.left = treeNode9;
        treeNode3.right = treeNode20;
        treeNode20.left = treeNode15;
        treeNode20.right = treeNode7;
        System.out.println(new Test37().maxDepth(treeNode3));
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    private int maxHeight = 0;

    public int maxDepth(TreeNode root) {
        preTravel(root, 0);
        return maxHeight;
    }

    private void preTravel(TreeNode root, int height) {
        if (root == null) {
            if (maxHeight <= height) {
                maxHeight = height;
            }
            return;
        }
        //左
        preTravel(root.left, height + 1);
        //右
        preTravel(root.right, height + 1);
    }

}
